package org.cooperativa.controller;

import org.cooperativa.entities.Persona;
import org.cooperativa.dao.PersonaFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.inject.Inject;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ActionEvent;
import org.cooperativa.controller.util.JsfUtil;
import org.cooperativa.entities.TransaccionDetalle;

@Named(value = "personaController")
@SessionScoped
public class PersonaController extends AbstractController<Persona> implements Serializable {

    @Inject
    private PersonaFacade ejbFacade;
    private Persona current;
    private boolean transaccionCreada = false;
    private ArrayList<Persona> personaList = new ArrayList<Persona>();

    public PersonaController() {
        super(Persona.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }

    public Persona getCurrent() {
        if (current == null) {
            current = new Persona();
            current.setFechaNacimiento(new Date());
        }
        return current;
    }

    public void setCurrent(Persona current) {
        this.current = current;
    }

    public boolean isTransaccionCreada() {
        return transaccionCreada;
    }

    public void setTransaccionCreada(boolean transaccionCreada) {
        this.transaccionCreada = transaccionCreada;
    }

    public ArrayList<Persona> getPersonaList() {
        return personaList;
    }

    public void setPersonaList(ArrayList<Persona> personaList) {
        this.personaList = personaList;
    }

    /**
     *
     * @param event
     */
    public void createNew(ActionEvent actionEvent) {
        String successMessage = ResourceBundle.getBundle("/Bundle").getString("TransaccionCreated");


        try {
            //antes de almacenar la nueva instacion debemos asignar los valores para auditoria de la BD
            current.setCreatedBy(1); //modificar luego para que ponga el id del usuario
            current.setCreationDate(new Date());

            ejbFacade.create(current);
            transaccionCreada = true;
            JsfUtil.addSuccessMessage(successMessage);
            
        } catch (EJBException ex) {
            String msg = "";
            Throwable cause = JsfUtil.getRootCause(ex.getCause());
            if (cause != null) {
                msg = cause.getLocalizedMessage();
            }
            if (msg.length() > 0) {
                JsfUtil.addErrorMessage(msg);
            } else {
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            }
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }



    }
}
